
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.1.0'
    }
}

apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.junit.platform.gradle.plugin'
mainClassName = "builder.Builder"
applicationName = "GUIslice"
archivesBaseName = 'builder'
version = '0.11.0-rc4' 
 
repositories {
    mavenCentral()
}

processResources { 
     from("src/main/java/") { 
         include 'resources/**' 
     } 
}

startScripts {
  def tplUnixName = 'scripts/customUnixStartScript.sh'
  def tplWinName = 'scripts/customWindowsStartScript.txt'
  assert project.file(tplUnixName).exists()
  assert project.file(tplWinName).exists()
  unixStartScriptGenerator.template = resources.text.fromFile(tplUnixName)
  windowsStartScriptGenerator.template = resources.text.fromFile(tplWinName)
}

jar {
  dependsOn junitPlatformTest
  libsDirName = "tmp/output/GUIsliceBuilder/lib"
  manifest {
    attributes("Main-Class": "builder.Builder",
    "Implementation-Title": "GUIsliceBuilder",
    "Implementation-Version": version)
  }
}

javadoc {
  source = sourceSets.main.allJava
  classpath = configurations.compile
	options.addBooleanOption('html5', true)
}

task createWindowsDir(type: Copy) {
  dependsOn jar, startScripts
  description = "create temp folder for windows distribution"
    from fileTree ("${buildDir}/tmp/output")
    into "${buildDir}/tmp/windows"
}

task copyWindowsScripts(type: Copy) {
  dependsOn createWindowsDir
  description = "copy start scripts and icons to tmp windows folder"
    from "${buildDir}/scripts/GUIslice.bat"
    into "${buildDir}/tmp/windows/GUIsliceBuilder"
    from "${projectDir}/scripts/guislicebuilder.ico"
    into "${buildDir}/tmp/windows/GUIsliceBuilder"
}

task copyLinuxDir(type: Copy) {
  dependsOn jar, startScripts
  description = "create temp folder for linux distribution"
    from fileTree ("${buildDir}/tmp/output")
    into "${buildDir}/tmp/linux"
}

task copyInstallScriptToLinux(type: Copy) {
  dependsOn copyLinuxDir
  description = "copy install script to linux temp folder"
    from "${projectDir}/scripts/install.sh"
    into "${buildDir}/tmp/linux/GUIsliceBuilder"
}

task copyArduinoResToLinux(type: Copy) {
  dependsOn copyLinuxDir
  description = "copy arduino resources to linux temp folder"
    from "${projectDir}/arduino_res"
    into "${buildDir}/tmp/linux/GUIsliceBuilder/arduino_res"
}

task copyLinuxResToLinux(type: Copy) {
  dependsOn copyLinuxDir
  description = "copy linux resources to linux temp folder"
    from "${projectDir}/linux_res"
    into "${buildDir}/tmp/linux/GUIsliceBuilder/linux_res"
}

task copyTemplatesToLinux(type: Copy) {
  dependsOn copyLinuxDir
  description = "copy templates to linux temp folder"
    from "${projectDir}/templates"
    into "${buildDir}/tmp/linux/GUIsliceBuilder/templates"
}

task copyLinuxScripts(type: Copy) {
  dependsOn copyInstallScriptToLinux, copyLinuxDir, copyArduinoResToLinux, copyLinuxResToLinux, copyTemplatesToLinux
  description = "copy start scripts and icons to linux temp folder"
    from "${projectDir}/scripts/desktop.template"
    into "${buildDir}/tmp/linux/GUIsliceBuilder/lib"
    from "${projectDir}/scripts/guislicebuilder.png"
    into "${buildDir}/tmp/linux/GUIsliceBuilder/lib"
    from "${buildDir}/scripts/GUIslice"
    into "${buildDir}/tmp/linux/GUIsliceBuilder/lib"
    rename ("GUIslice", "startup.template")
  doLast {
    mkdir "${buildDir}/tmp/linux/GUIsliceBuilder/projects"
    mkdir "${buildDir}/tmp/linux/GUIsliceBuilder/logs"
  }
}

task copyMacOSDir(type: Copy) {
  dependsOn jar, startScripts
  description = "create temp folder for macos distribution"
  doFirst {
    mkdir "${buildDir}/tmp/macApp/GUIsliceBuilder"
    mkdir "${buildDir}/tmp/macApp/GUIsliceBuilder/projects"
    mkdir "${buildDir}/tmp/macApp/GUIsliceBuilder/logs"
  }
  from fileTree ("${buildDir}/tmp/output/GUIsliceBuilder/lib")
  into "${buildDir}/tmp/macApp/GUIsliceBuilder"
}

task copyArduinoResToMacOS(type: Copy) {
  dependsOn copyMacOSDir
  description = "copy arduino resources to macApp temp folder"
    from "${projectDir}/arduino_res"
    into "${buildDir}/tmp/macApp/GUIsliceBuilder/arduino_res"
}

task copyLinuxResToMacOS(type: Copy) {
  dependsOn copyMacOSDir
  description = "copy linux resources to macApp temp folder"
    from "${projectDir}/linux_res"
    into "${buildDir}/tmp/macApp/GUIsliceBuilder/linux_res"
}

task copyTemplatesToMacOS(type: Copy) {
  dependsOn copyMacOSDir
  description = "copy templates to macApp temp folder"
    from "${projectDir}/templates"
    into "${buildDir}/tmp/macApp/GUIsliceBuilder/templates"
}

task buildMacOSInstall(type: Copy) {
  dependsOn copyMacOSDir, copyArduinoResToMacOS, copyLinuxResToMacOS, copyTemplatesToMacOS
  description = "copy start scripts and icons to macApp temp folder"
    from "${buildDir}/scripts/GUIslice.icns"
    into "${buildDir}/tmp/macApp/GUIsliceBuilder"
}

task buildWinInstall() { 
  dependsOn copyWindowsScripts
  description = "create windows distribution from windows temp folder"
  group = "build"
  def innoSetupDir = new File("${buildDir}/innosetup")
 
  delete innoSetupDir  
  innoSetupDir.mkdir();  
  
  copy {
    from("${projectDir}/scripts/setup_template.iss")
    rename("setup_template.iss", "setup.iss")
    expand([
      applicationVersion: "${version}",
    ])
    into(innoSetupDir)
  }
  doLast {
    exec {
    workingDir projectDir
    commandLine "ISCC.exe ${innoSetupDir}/setup.iss /q".split()
    }
  }
}

task packageWinDistrib(type: Zip) {
  dependsOn buildWinInstall
  group = "build"
  description = "windows package distribution"
  from "${buildDir}/innosetup/builder-" + "win-" + version + ".exe"
  archiveName = "builder-" + "win-" + version + ".zip"
  destinationDir = file("${buildDir}/disp")
}

task packageLinuxDistrib(type: Tar) {
  dependsOn copyLinuxScripts
  description = "create linux distribution from linux temp folder"
  group = "build"
  description = "linux package distribution"
  from ("${buildDir}/tmp/linux") {
   exclude ("**/*.sh") // 1
   fileMode 0644
  }
  from ("${buildDir}/tmp/linux") {
   include ("**/*.sh")
   fileMode 0755
  }
  baseName = "builder-" + "linux"
  destinationDir = file("${buildDir}/disp")
  extension = "tar.gz"
  compression = Compression.GZIP  
}

task packageMacOSDistrib(type: Zip) {
  dependsOn buildMacOSInstall
  group = "build"
  description = "MacOS package distribution"
  from "${buildDir}/tmp/macApp"
  archiveName = "builder-" + "osx-" + version + ".zip"
  destinationDir = file("${buildDir}/disp")
}

task packageAll() {
  dependsOn packageWinDistrib, packageLinuxDistrib, packageMacOSDistrib
  group = "assemble"
  description = "package all distributions"
}

dependencies {
  testCompile ('junit:junit:4.12',
               'org.assertj:assertj-core:3.11.1')
  testImplementation(
            'org.junit.jupiter:junit-jupiter-api:5.1.0',
            'org.assertj:assertj-core:3.11.1'
  )
  testRuntime(
            'org.junit.jupiter:junit-jupiter-engine:5.1.0',
            'org.junit.vintage:junit-vintage-engine:5.1.0',
            'org.junit.platform:junit-platform-launcher:1.1.0',
            'org.junit.platform:junit-platform-runner:1.1.0'
  )
} 

